package com.example.recyclerviewdemo2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {

    private Context context;
    private List<ContactModel> list;

    public MyAdapter(Context context) {
        this.context = context;
    }

    public void setList(List<ContactModel> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyHolder(LayoutInflater.from(context).inflate(R.layout.list_item_contact,null));
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyHolder holder, int position) {
        holder.contactName.setText(list.get(position).getContactName());
        holder.contactIconResId.setImageResource(list.get(position).getContactIconResId());

    }

    @Override
    public int getItemCount() {
        return null!=list? list.size() : 0;
    }

    class MyHolder extends RecyclerView.ViewHolder{
        TextView contactName;
        ImageView contactIconResId;
        public MyHolder(View itemView){
            super(itemView);
            contactName=itemView.findViewById(R.id.list_item_contact_name);
            contactIconResId=itemView.findViewById(R.id.list_item_contact_icon);

        }
    }
}
